# include <iostream>
# include <cv.h>
# include <highgui.h>
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;
using namespace std;

int gerade(IplImage * img, double a, double b, int gray)
{
//y=ax+b
    int width = img->width+1;
    int height = img->height;

    for(int i=0;i<width;i++)
    {
        if (((i*a+b)*width + i)<(width*height))
            img->imageData[(int)((i*a+b)*width + i)] = 255;
    }
    return 0;
}

int main (int argc , char * argv []) {

 IplImage * img = cvLoadImage (argv [1], CV_LOAD_IMAGE_GRAYSCALE) ;
 if (! img ) {
    cerr << " Could not load image file : " << argv [1] << endl ;
    exit ( EXIT_FAILURE );
 }

 printf("Grauwerte von 0x100: %d\n", (unsigned char) img->imageData[100]);


int k = 100;

int longueur = img->width ;

cout << longueur << endl;
 for (int i=0; i<20 ; i++)
 {
   for (int j=0; j<20 ; j++)
   {
      img->imageData[i*(longueur+1) + j] = 255;
   }
 }

//gerade(img, 0, img->height, 255);

//Canny(img, img, 30, 200, 3);

// allocate a 320x240 color image and fill it with green (in RGB space)
Mat_<Vec3b> imgv(240, 320, Vec3b(0,255,0));
// now draw a diagonal white line
for(int i = 0; i < 100; i++)
imgv(i,i)=Vec3b(255,255,255);

imshow("Diagonal Line", imgv);

// and now scramble the 2nd (red) channel of each pixel
for(int i = 0; i < imgv.rows; i++)
for(int j = 0; j < imgv.cols; j++)
imgv(i,j)[2] ^= (uchar)(i ^ j);
imshow("Scrambled Line", imgv);

//######################################################

Mat imgx(img); // convert IplImage* -> Mat

 for (int i=0; i<20 ; i++)
 {
   for (int j=0; j<20 ; j++)
   {
      //img->imageData[i*(longueur+1) + j] = 255;
        imgx.at<uchar>(j,i) = 0;//.copyTo(A.row(i));
   }
 }
// sharpen image using "unsharp mask" algorithm
Mat blurred; double sigma = 1, threshold = 5, amount = 1;
GaussianBlur(imgx, blurred, Size(), sigma, sigma);
Mat lowConstrastMask = abs(imgx - blurred) < threshold;
Mat sharpened = imgx*(1+amount) + blurred*(-amount);
imgx.copyTo(sharpened, lowConstrastMask);


 //cvSaveImage("lena2.jpg",imgx);

 cvNamedWindow (" original ", CV_WINDOW_AUTOSIZE );
// cvShowImage (" original ", imgx );
imshow("rectangles", sharpened);

 cvWaitKey (0);

 cvDestroyWindow (" original ");
 //cvReleaseImage (& imgx );

Mat image(200, 200, CV_8UC3, Scalar(0));
RotatedRect rRect = RotatedRect(Point2f(100,100), Size2f(100,50), 30);
Point2f vertices[4];
rRect.points(vertices);
for (int i = 0; i < 4; i++)
line(image, vertices[i], vertices[(i+1)%4], Scalar(0,255,0));
Rect brect = rRect.boundingRect();
rectangle(image, brect, Scalar(255,0,0));
imshow("rectangles", image);
waitKey(0);




 return EXIT_SUCCESS ;
}
